package net.janeluo.rs.service;

import java.util.List;

import net.janeluo.rs.entity.RS_Menus;
import net.janeluo.rs.entity.RS_Shops;

import org.apache.commons.lang.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.sql.Criteria;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.service.Service;

/**
 * 菜单管理处理业务类
 * 
 * @author DLK)YangYining
 * @version 1.0.0
 */
@IocBean(fields = { "dao" })
public class MenusManagerService extends Service {
    private static final Log log = Logs.getLog(MenusManagerService.class);
    
    public MenusManagerService() {
        super();
    }

    public MenusManagerService(Dao dao) {
        super(dao);
    }
    
    /**
     * 菜单新增
     * 
     * @param entity 菜单实体
     * @return 菜单实体
     */
    public RS_Menus insertMenu(RS_Menus entity){
        
        RS_Shops shops = dao().fetch(RS_Shops.class, entity.getSid());
        entity.setSname(shops.getSname());
        
        return dao().insert(entity);
    }
    
    /**
     * 菜单更新
     * 
     * @param entity 菜单实体
     * @return 菜单实体
     */
    public int updateMenu(RS_Menus entity){
        return dao().update(entity);
    }
    
    /**
     * 根据菜单id查询菜单
     * 
     * @param mid 菜单id
     * @return 菜单信息实体
     */
    public RS_Menus findMenuById(Integer mid) {
        return dao().fetch(RS_Menus.class, mid);
    }
    
    /**
     * 根据菜单id查询菜单
     * 
     * @param sid 菜单id
     * @return 菜单信息实体
     */
    public void deleteMenu(Integer sid) {
        dao().delete(RS_Menus.class, sid);
    }
    
    /**
     * 根据菜单id查询菜单
     * 
     * @param sidarr 菜单id集合
     * @return 菜单信息实体
     */
    public void deleteMenus(Integer[] sidarr) {
        dao().clear(RS_Menus.class, Cnd.where("mid", "in", sidarr));
    }
    
    /**
     * 查询菜单列表 
     * 
     * @param menuName 菜单名称
     * @return
     */
    public List<RS_Menus> getMenusList (Integer sid, String menuName){
        log.debug("================= 查询列表处理开始 ==================");
        
        Criteria cri = Cnd.cri();
        if (sid != null) {
            cri.where().andEquals("sid", sid);
        }
        if (StringUtils.isNotEmpty(menuName)) {
            cri.where().andLike("mname", "%" + menuName + "%");
        }
        cri.getOrderBy().asc("sname");
        List<RS_Menus> list = dao().query(RS_Menus.class, cri);
        log.debug("================= 查询列表处理结束 ==================");
        return list;  
    }
    
    /**
     * 查询店铺列表
     * 
     * @return
     */
    public List<RS_Shops> getShopList(){
        return dao().query(RS_Shops.class, Cnd.cri());
    }
}
